#include "TimeOperation.h"

#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
TimeOperation::TimeOperation() {}

std::string TimeOperation::current_formatted_data()
{
    // 获取当前时间点
    auto now = std::chrono::system_clock::now();

    // 转换为 time_t（秒精度）
    auto now_time_t = std::chrono::system_clock::to_time_t(now);

    // 提取毫秒部分
    auto duration = now.time_since_epoch();
    auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration) % 1000;

    // 转换为本地时间
    std::tm local_time;
    localtime_s(&local_time, &now_time_t);

    // 格式化输出
    std::ostringstream oss;
    oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S")             // 格式化到秒
        << "." << std::setfill('0') << std::setw(3) << millis.count(); // 添加毫秒部分

    return oss.str();
}

uint64_t TimeOperation::timestamp()
{
    time_t current_time;
    time(&current_time);
    auto current_time_uint64 = static_cast<uint64_t>(current_time);
    return current_time_uint64;
}
